module.exports = function ($scope, $resource, $modalInstance, bean, toaster, FileUploader) {
    console.log(bean);
    $scope.bean = bean;
    $scope.bean.nsubsidy_price = $scope.bean.subsidy_price * 0.01;
    $scope.label_list = []
    if($scope.bean.guide_price == 0 || $scope.bean.cost_price == 0){
        isopen();
    }
    function isopen(){
        var para = {
            sale_code:$scope.bean.product_code
        };
        $resource('/api/as/tc/salefree/info', {}, {}).save(para, function (res) {
            if (res.errcode === 0 && res.data.num > 0) {
                $scope.isfree = true;
            } else {
               $scope.isfree = false;
            }
        });
    }

     
    $resource('/api/as/mc/shoplabel/findShopLabelList', {}, {}).save('', function (res) {
      if (res.errcode == 0) {
          $scope.labe = res.data;
      }
    });
    $scope.idsload = function() {
      $resource('/api/ac/mc/ShopLabelService/findLabelList', {}, {}).save(bean, function (res) {
        if (res.errcode == 0) {
          if(res.data!=""){
            $scope.label_list = res.data;
          }
        }
      });
    }
    $scope.idsload();

    $scope.add = function (lab) {
      $scope.idsselid = lab.id;
      for(var i = 0; i < $scope.label_list.length; i++){
        if($scope.idsselid == $scope.label_list[i].id){
            toaster.error({title: '', body: '不能重复添加'});
            return
        }
      }
      $scope.label_list.push({'id':lab.id,'label':lab.label});
    }

    $scope.remove = function (index) {
      $scope.label_list.splice(index,1)
    };
    //更改是否允许0元领取
    $scope.changefree = function (sale_code) {
        var url =  $scope.isfree ? '/api/as/tc/salefree/delete' : '/api/as/tc/salefree/create';
        var para = {
            sale_code:sale_code
        };
        $resource(url, {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $scope.isfree = !$scope.isfree;
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //添加影院
    $scope.ok = function () {
        if($scope.bean.product_profit_set == 'product_set'){
			if ($scope.bean.profit_level1 + $scope.bean.profit_level2 + $scope.bean.profit_level3 != 100) {
				toaster.error({title: '', body: '三级分润总和应为100%'});
				return;
			}
		} else {
			if ($scope.bean.shop_profit_level1 + $scope.bean.shop_profit_level2 + $scope.bean.shop_profit_level3 != 100) {
				toaster.error({title: '', body: '三级分润总和应为100%'});
				return;
			}
		}
        $scope.beaninfo = $scope.bean;
        $scope.beaninfo.subsidy_price = $scope.bean.nsubsidy_price * 100;
        //加入labelids
        var label_ids = ''
        for(var i=0;i<$scope.label_list.length;i++){
          if(i==0){
            label_ids = $scope.label_list[i].id;
          }else{
            label_ids = label_ids + ',' + $scope.label_list[i].id;
          }
        }
        $scope.beaninfo.label_ids = label_ids;
        if($scope.beaninfo.product_profit_set == 'default_shop'){
            $scope.beaninfo.profit_level1 = null;
            $scope.beaninfo.profit_level2 = null;
            $scope.beaninfo.profit_level3 = null;
            $scope.beaninfo.profit_percent = null;
        }
        $resource('/api/as/mc/shopproduct/updateMyProduct', {}, {}).save($scope.beaninfo, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
                $modalInstance.close();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //取消
    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }

};